package com.fintech.third.utils.wechat;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;


public class DateUtil {
	
	public static int YEAR = Calendar.YEAR;
	public static int MONTH = Calendar.MONTH;
	public static int DAY = Calendar.DAY_OF_MONTH;
	public static int MINUTE = Calendar.MINUTE;
	public static int SECOND = Calendar.SECOND;	
	/**
	 * 
	* @Title: addTime 
	* @Description:  
	* @param date 时间
	* @param amount 增加的时间量
	 */
	public static Date addTime(Date date,int amount,int type){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(type, amount);
		return cal.getTime();
	}

	
	/**
	 * 
	 * 描述：获取当前时间前几个月时间
	 * 作者：邱霞
	 * 时间：2011-10-21 上午10:52:37
	 * @param date 当前时间 yyyy-MM-dd字符串
	 * @param count 月数
	 * @return 返回yyyy-MM-dd字符串
	 */
	public static String getBeforeDate(String date, int count)
	{
		
		String[] item = (date.substring(0, 10)).split("-");
		int year = Integer.parseInt(item[0]);
		int month = Integer.parseInt(item[1]);
		int day = Integer.parseInt(item[2]);
		if ((month - count) <= 0)
		{
			month = month + 12 - count;
			year = year - 1;
		}
		else
		{
			month = month - count;
		}
		if (month < 10)
		{
			return year + "-" + "0" + month + "-" + day;
		}
		else
		{
			return year + "-" + month + "-" + day;
		}
		
	}
	
	/**
	 * 
	 * 描述：获取当前时间未来几个月时间
	 * 作者：邱霞
	 * 时间：2011-10-21 上午10:52:37
	 * @param date 当前时间 yyyy-MM-dd字符串
	 * @param count 月数
	 * @return 返回yyyy-MM-dd字符串
	 */
	public static String getAfterDate(String date, int count)
	{
		String[] item = (date.substring(0, 10)).split("-");
		int year = Integer.parseInt(item[0]);
		int month = Integer.parseInt(item[1]);
		int day = Integer.parseInt(item[2]);
		if ((month + count) > 12)
		{
			month = month - 12 + count;
			year = year + 1;
		}
		else
		{
			month = month + count;
		}
		if (month < 10)
		{
			if(day<10){
				return year + "-" + "0" + month + "-" + "0" + day;
			}else{
				return year + "-" + "0" + month + "-" + day;
			}
		}
		else
		{
			if(day<10){
				return year + "-" + month + "-" + "0" + day;
			}else{
				return year + "-" + month + "-" + day;
			}
		}
	}
	
	/**
	 * 
	 * @描述：格式化日期，返回yyyy-MM-dd
	 * @作者：李炜
	 * @时间：2011-11-3 下午07:03:37
	 * @param date
	 * @return
	 */
	public static String formatDate(String date)
	{
		if (date == null || date.length() != 8)
		{
			return date;
		}
		StringBuffer buffer = new StringBuffer();
		buffer.append(date.subSequence(0, 4));
		buffer.append("-");
		buffer.append(date.subSequence(4, 6));
		buffer.append("-");
		buffer.append(date.subSequence(6, 8));
		return buffer.toString();
	}
	
	/**
     * 描述：解析日期字串
     * 
     * @param dateStr
     *            日期字串
     * @return 按 yyyy-MM-dd HH:mm:ss 格式解析
     */
    public static Date parseString(String dateStr)
    {
        return parseString(dateStr, "HH:mm:ss");
    }

    /**
     * 描述：解析日期字串
     * 
     * @param dateStr
     *            日期字串
     * @param pattern
     *            字串日期格式
     * @return 对应日期类型数据
     */
    public static Date parseString(String dateStr, String pattern)
    {
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        try
        {
            if (!StringUtils.isEmpty(dateStr))
            {
                return dateFormat.parse(dateStr);
            }
        } catch (ParseException ex)
        {
           ex.printStackTrace();
        }
        return null;
    }
    
    /**
     * 
     * @Title: getDateStr   
     * @Description:  获取时间字符串
     * @param:      
     * @return: String      
     * @throws
     */
    public static String getDateStr(Date date,String pattern){
    	SimpleDateFormat sb = new SimpleDateFormat(pattern);
    	return sb.format(date);
    }
	

}
